<template>
  <div class="app-container">
    <el-form autoComplete="on" :model="form" :rules="rules" ref="form" label-width="120px">
      <el-form-item label="管理员名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="mail">
        <el-input v-model="form.mail" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input type="password" v-model="form.password2" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择">
          <el-option v-for="(role, key) in roles" :key="key" :label="role" :value="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管理员状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">冻结</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <!-- <el-button @click="onCancel">取消</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { postAdd } from '@/api/admin'
import { getOpt } from '@/api/role'
import { isMobile } from '@/utils/validate'

export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (this.error && this.error.param === 'username') {
        callback(new Error(this.error.message || '服务器错误'))
        delete this.error
      }
      if (value.length < 4) {
        callback(new Error('用户名不能少于5位'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('确认密码不一致'))
      } else {
        callback()
      }
    }
    const validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号码格式错误'))
      } else {
        callback()
      }
    }
    const validateRole = (rule, value, callback) => {
      console.log(value)
      if (!value) {
        callback(new Error('请选择角色'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        mobile: '',
        password: '',
        password2: '',
        mail: '',
        role: '',
        status: 0
      },
      roles: {},
      rules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        mobile: [{ required: true, trigger: 'blur', validator: validateMobile }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        password2: [{ required: true, trigger: 'blur', validator: validatePass2 }],
        role: [{ required: true, trigger: 'blur', validator: validateRole }]
      }
    }
  },
  created() {
    getOpt(opt => {
      this.roles = opt
    })
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          console.log('error submit!!')
          return false
        }
        var post = Object.assign({}, this.form)
        delete post.password2
        postAdd(post).then(res => {
          if (res.code !== 30000) {
            return this.$message('添加失败!')
          }
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.$router.replace('/admin/list')
        }).catch(err => {
          this.error = err.error
          this.$refs.form.validate()
        })
      })
    },
    onCancel() {
      this.$message({
        message: '已取消添加!',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
